var mlf;

/**
 * 设置结果接收对象
 * @param _opinionPrice
 */
function setModel(_mlf) {
    if (_mlf == undefined || !(_mlf instanceof com.jf.crawl.model.OpinionMlf)) {
        throw new java.lang.Exception("mlf 对象为空");
    }
    log.info("mlf {}", _mlf)
    mlf = _mlf;
}

function crawler() {
    var host = "http://www.pbc.gov.cn";
    var html = Post("http://www.pbc.gov.cn/zhengcehuobisi/125207/125213/125431/125475/17081/index1.html");
    if (html.contains("不开展公开市场操作")) {
        log.info("{},不开展公开市场操作", message)
        return true;
    }else if(html.contains("请开启JavaScript并刷新该页")){
        log.info("{},请开启JavaScript并刷新该页", message)
        return false;
    }
    var doc = Jsoup.parse(html);
    var table = doc.select("div[opentype='page'] > table table").get(0);
    var message = table.select(".hui12").text();
    if (message.substr(message.length - 2, 2) != getToday()) {
        log.info("{},不是今天的数据不抓取", message)
        return true;
    }
    var href = table.select("a").attr("href");
    html = Post(host + href);
    if (html.contains("不开展公开市场操作")) {
        log.info("{},不开展公开市场操作", message)
        return true;
    }else if(html.contains("请开启JavaScript并刷新该页")){
        log.info("{},请开启JavaScript并刷新该页", message)
        return false;
    }
    var doc = Jsoup.parse(html);
    var tips = doc.select("#zoom > p[align='center']");
    var tables = doc.select("#zoom table");
    for (var i = 0; i < tips.size(); i++) {
        var tip = tips.get(i).text();
        if (tip == "") {
            tips.remove(i);
        }
    }
    log.info("{},{}", tips.size(), tables.size());
    if (tips.size() != tables.size()) {
        log.info("新情况");
        return false;
    }
    for (var i = 0; i < tips.size(); i++) {
        var tip = tips.get(i).text();
        var table = tables.get(i);
        if (tip.contains("逆回购") || tip.contains("MLF")) {
            var timeIndex = 0;
            var priceIndex = 0;
            var trs = table.select("tr");
            if (trs.size() > 1) {
                var tr = trs.get(0);
                var tds = tr.select("td");
                for (var j = 0; j < tds.size(); j++) {
                    var txt = tds.get(j).text();
                    if (txt.contains("期限")) {
                        timeIndex = j;
                    } else if (txt.contains("利率")) {
                        priceIndex = j;
                    }
                }
                for (var k = 1; k < trs.size(); k++) {
                    tds = trs.get(k).select("td");
                    if (tds.size() >= (timeIndex > priceIndex ? timeIndex : priceIndex)) {
                        var time = tds.get(timeIndex).text();
                        var price = tds.get(priceIndex).text().replace("%", "");
                        if (time.contains("7天")) {
                            mlf.setRp7Day(commonUtil.string2Decimal(price));
                        } else if (time.contains("14天")) {
                            mlf.setRp14Day(commonUtil.string2Decimal(price));
                        } else if (time.contains("28天")) {
                            mlf.setRp28Day(commonUtil.string2Decimal(price));
                        } else if (time.contains("3个月")) {
                            mlf.setMlf3Month(commonUtil.string2Decimal(price));
                        } else if (time.contains("6个月")) {
                            mlf.setMlf6Month(commonUtil.string2Decimal(price));
                        } else if (time.contains("1年")) {
                            mlf.setMlf1Year(commonUtil.string2Decimal(price));
                        }
                    }
                }
            }
        }
    }
    return true;
}

function getToday() {
    var date = new Date();
    var day = date.getDate();
    return day;
}